/*
 * Copyright 2011-2013, David George, Licensed under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.magneato.utils.parsers;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

/**
 * Discover any attributes marked with isFacet tag in template files Turn other
 * content into plain text
 * 
 * attributes: isFacet - it is a facet, true - index, false don't index?
 * 
 * @author David B George
 */
public class FacetIndexerParser extends org.xml.sax.helpers.DefaultHandler {
	//private ArrayList<Facet> facets = new ArrayList<Facet>();
	private HashMap<String,String> facets = new HashMap<String,String>();
	private StringBuilder data = new StringBuilder();;
	private StringBuilder contents = new StringBuilder();;
	private String facetName;
	private DateFormat dateFormat;
	private long date = -1;
	private boolean searchable = false;

	public void startElement(String uri, String localName, String qName,
			Attributes atts) throws SAXException {

		for (int i = 0; i < atts.getLength(); i++) {
			if ("isFacet".equalsIgnoreCase(atts.getLocalName(i))
					&& "true".equalsIgnoreCase(atts.getValue(i))) {
				facetName = qName;
			} else if ("date".equalsIgnoreCase(atts.getLocalName(i))) {
				String format = atts.getValue(i);
				dateFormat = new SimpleDateFormat(format);
			} else if ("searchable".equalsIgnoreCase(atts.getLocalName(i))
					&& "true".equalsIgnoreCase(atts.getValue(i))) {
				searchable = true;
			}
		}
	}

	public void endElement(String uri, String localName, String qName)
			throws SAXException {

		if (facetName != null && facetName.equals(qName)) {
			// it is a facet
			if (!facets.containsKey(qName)) {
				facets.put(qName, data.toString());
			} else {
				contents.append(" " + qName + ":" + data.toString());
			}
			facetName = null;
		} else if (dateFormat != null) {
			try {
				Date d = dateFormat.parse(data.toString());
				date = d.getTime() / 1000;
				dateFormat = null;
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// date
		} else {
			// other content
			if (searchable) {
				contents.append(" " + data);
				searchable = false;
			}
		}
		data.setLength(0); // moderately faster than reallocating
	}

	public void characters(char[] chars, int start, int length) {
		data.append(new String(chars, start, length).trim());
	}

	public Set<Entry<String,String>> getFacets() {
		return facets.entrySet();
	}

	public String getContents() {
		return contents.toString();
	}

	public long getDate() {
		return date;
	}
}
